package com.pr.makfilm.user.pages.room.create.preview;

import com.google.common.collect.Table;
import com.pr.makfilm.domain.NumberOrderEnum;
import com.pr.makfilm.domain.Room;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 09.10.13
 * Time: 17:42
 * To change this template use File | Settings | File Templates.
 */
public class PreviewRowRepeater extends RepeatingView
{
   private IModel<Table> placesModel;

   private final PropertyModel<List<String>> yHeaderModel;

   public PreviewRowRepeater(String id, IModel<Room> model,
                             IModel<Table> placesModel,
                             PropertyModel<List<String>> yHeaderModel)
   {
      super(id, model);
      this.placesModel = placesModel;
      this.yHeaderModel = yHeaderModel;
   }

   @Override
   protected void onPopulate()
   {
      removeAll();
      Room room = (Room) getDefaultModelObject();
      List<String> headers = yHeaderModel.getObject();
      int j = room.getyOrder() == NumberOrderEnum.BEGINNING ? 0 : headers.size() - 1;
      for (int i = 0; i < room.getRowCount(); i++)
      {
         add(new PreviewRow(newChildId(), getDefaultModel(), placesModel, i, headers.get(j)));
         if (room.getyOrder() == NumberOrderEnum.BEGINNING)
         {
            j++;
         }
         else
         {
            j--;
         }
      }
   }

   @Override
   protected void onDetach()
   {
      super.onDetach();
      placesModel.detach();
      yHeaderModel.detach();
   }
}
